Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expression: Fix some datetime related cases that is inconsistent with MySQL #7636

Merged
merged 6 commits into from
Sep 7, 2018

Conversation

spongedu
Copy link
Contributor

@spongedu spongedu commented Sep 6, 2018

What problem does this PR solve?

This pr fixes some datetime related cases in TiDB that is inconsistent with MySQL.

  1. Currently in TiDB, sql mode NO_ZERO_DATE is not taken into consideration in some builtin-time functions(month, dayofmonth, year, monthname). In MySQL, if NO_ZERO_DATE is not set, no error or warning should happen on zero datetime such as 0000-00-00 00:00:00.
    For example, in MySQL:
mysql> drop table t;
Query OK, 0 rows affected (0.03 sec)

mysql> create table t ( a bigint);                                                                                                                                                                          Query OK, 0 rows affected (0.04 sec)

mysql> select @@sql_mode;
+--------------------------------------------+
| @@sql_mode                                 |
+--------------------------------------------+
| STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION |
+--------------------------------------------+
1 row in set (0.00 sec)

mysql> insert into t select month("0000-00-00 00:00:00");
Query OK, 1 row affected (0.01 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> set sql_mode="NO_ZERO_DATE";                                                                                                                                                                         Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> insert into t select month("0000-00-00 00:00:00");
Query OK, 1 row affected, 1 warning (0.01 sec)
Records: 1  Duplicates: 0  Warnings: 1

mysql> show warnings;
+---------+------+-------------------------------------------------+
| Level   | Code | Message                                         |
+---------+------+-------------------------------------------------+
| Warning | 1292 | Incorrect datetime value: '0000-00-00 00:00:00' |
+---------+------+-------------------------------------------------+
1 row in set (0.00 sec)

mysql> set sql_mode="NO_ZERO_DATE,STRICT_TRANS_TABLES";                                                                                                                                                     Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> insert into t select month("0000-00-00 00:00:00");                                                                                                                                                   ERROR 1292 (22007): Incorrect datetime value: '0000-00-00 00:00:00'

While in TiDB:

tidb> select @@sql_mode;
+--------------------------------------------+
| @@sql_mode                                 |
+--------------------------------------------+
| STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION |
+--------------------------------------------+
1 row in set (0.00 sec)

tidb> create table t ( a bigint);
Query OK, 0 rows affected (0.01 sec)

tidb> insert into t select month("0000-00-00 00:00:00");
ERROR 1292 (22007): Incorrect datetime value: '0000-00-00 00:00:00.000000'
tidb> set sql_mode="NO_ZERO_DATE";
Query OK, 0 rows affected (0.00 sec)

tidb> insert into t select month("0000-00-00 00:00:00");
Query OK, 1 row affected, 1 warning (0.00 sec)

tidb> show warnings;
+---------+------+--------------------------------------------------------+
| Level   | Code | Message                                                |
+---------+------+--------------------------------------------------------+
| Warning | 1292 | Incorrect datetime value: '0000-00-00 00:00:00.000000' |
+---------+------+--------------------------------------------------------+
1 row in set (0.00 sec)

tidb> set sql_mode="NO_ZERO_DATE,STRICT_TRANS_TABLES";
Query OK, 0 rows affected (0.00 sec)

tidb> insert into t select month("0000-00-00 00:00:00");
ERROR 1292 (22007): Incorrect datetime value: '0000-00-00 00:00:00.000000'
  1. Currently in TiDB, float values are not correctly cast into datetime.
    For example, in MySQL:
mysql> select cast(20170118.999 as datetime);
+--------------------------------+
| cast(20170118.999 as datetime) |
+--------------------------------+
| 2017-01-18 00:00:00            |
+--------------------------------+
1 row in set (0.00 sec)

While in TiDB:

tidb> select cast(20170118.999 as datetime);
ERROR 1292 (22007): invalid time format: '99'

What is changed and how it works?

Changed the way some builtin_time functions deal with ZERO datetime

Check List

Tests

Unit tests && integration tests

Copy link
Contributor

@eurekaka eurekaka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@@ -1175,7 +1175,7 @@ func (b *builtinCastStringAsTimeSig) evalTime(row chunk.Row) (res types.Time, is
sc := b.ctx.GetSessionVars().StmtCtx
res, err = types.ParseTime(sc, val, b.tp.Tp, b.tp.Decimal)
if err != nil {
return res, false, errors.Trace(err)
return res, false, err
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason for removing the Trace?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eurekaka nope. This should be change by mistake. I'll fix here.

@zz-jason
Copy link
Member

zz-jason commented Sep 7, 2018

/run-all-tests

@zz-jason zz-jason added status/LGT1 Indicates that a PR has LGTM 1. contribution This PR is from a community contributor. component/expression labels Sep 7, 2018
Copy link
Member

@zz-jason zz-jason left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@zz-jason zz-jason added status/LGT2 Indicates that a PR has LGTM 2. and removed status/LGT1 Indicates that a PR has LGTM 1. labels Sep 7, 2018
@zz-jason zz-jason merged commit 6fb1a63 into pingcap:master Sep 7, 2018
@spongedu spongedu deleted the dc_0906 branch September 7, 2018 06:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component/expression contribution This PR is from a community contributor. status/LGT2 Indicates that a PR has LGTM 2. type/compatibility
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants